package aor2.cpu.blockregisters;

import aor2.components.Element;
import aor2.components.ElementIO;
import aor2.components.Register;
import aor2.components.Restorable;
import aor2.cpu.InternBus;
import aor2.cpu.blockcontrol.ControlSignalSet;

public enum TempRegisters implements Element, ElementIO, Restorable {
	A(0, InternBus.M2, InternBus.M2), B(0, InternBus.M2, InternBus.M2);

	static {
		ControlSignalSet css = ControlSignalSet.getInstance();
		css.get("Ain").addDestinationElementIO(A);
		css.get("Bin").addDestinationElementIO(B);
		css.get("Aout").addSourceElementIO(A);
		css.get("Bout").addSourceElementIO(B);
	}

	private Register reg;

	@Override
	public long getState() {
		return reg.getState();
	}

	@Override
	public void setState(long state) {
		reg.setState(state);
	}

	public void out() {
		reg.out();
	}

	public void in() {
		reg.in();
	}

	private TempRegisters(long state, Element input, Element output) {
		this.reg = new Register(state, input, output);
	}

	public static String info() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("A[" + A.getState() + "]\n");
		buffer.append("B[" + B.getState() + "]");
		return buffer.toString();
	}

	@Override
	public void restore(String restorePoint) {
		String trim = restorePoint.replace(beginTag(), "");
		trim = trim.replace(endTag(), "");
		trim = trim.trim();
		String[] states = trim.split(" ");

		A.reg.setState(Long.parseLong(states[0]));
		B.reg.setState(Long.parseLong(states[1]));

	}

	@Override
	public String createRestorePoint() {
		return beginTag() + "\n\t" + A.reg.getState() + " " + B.reg.getState()
				+ " " + "\n" + endTag();
	}

	@Override
	public String beginTag() {
		return "<A&B>";
	}

	@Override
	public String endTag() {
		return "</A&B>";
	}
}
